package cn.hls.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.hls.dao.CarDao;
import cn.hls.dao.CertSetDao;
import cn.hls.dao.HandCarDao;
import cn.hls.entity.Car;
import cn.hls.entity.CertSet;
import cn.hls.entity.HandCar;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CarService;

/**
 * @fileName: CarServiceImpl.java
 * @package: cn.christ.service.impl
 * @Description: 车辆基本信息业务逻辑接口实现类
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Service("carService")
public class CarServiceBean extends BaseServiceBean<Car> implements
		CarService {
	@Resource(name = "carDao")
	private CarDao dao;
	@Resource(name="handCarDao")
	private HandCarDao handCarDao;//挂车数据库对象
	@Resource(name="certSetDao")
	private CertSetDao certSetDao;
	@Override
	public boolean add(Car entity) throws Exception {
		boolean flag=super.add(entity);
		if(StringUtils.hasText(entity.getLicenseNum2())){
			HandCar handCar=handCarDao.loadHandCarByLicenseNum(entity.getLicenseNum2());
			if(handCar!=null){
				handCar.setCar(entity);
				flag&=handCarDao.update(handCar);
			}
		}
		return flag;
	}
	@Override
	public boolean update(Car entity) throws Exception {
		Car tempEntity = dao.find(entity.getId());
		Date createTime = tempEntity.getCreateTime();
		BeanUtils.copyProperties(entity, tempEntity);
		tempEntity.setCreateTime(createTime);
		tempEntity.setUpdateTime(new Date());
		return super.update(tempEntity);
	}

	@Override
	public void delete(Object entityId) throws Exception {
		dao.delete(entityId);
	}

	@Override
	public PageInfo<Car> loadCarInfo(Car entity,PageInfo<Car> page) throws Exception {
		return dao.loadCarInfo(entity, page);
	}

	@Override
	public List<Car> loadCarList(Car entity) throws Exception {
		return dao.loadCarList(entity);
	}

	@Override
	public boolean exists(Car entity) throws Exception {
		return dao.exists(entity);
	}
	
	@Override
	public Map<String,Integer>loadExpiredCar(Long companyId)throws Exception{
		return dao.loadExpiredCar(companyId,certSetDao.loadcertSetInfoToMap(null));
	}
	@Override
	public List<Car> loadExpiredCarList(Car entity,
			Map<String, CertSet> certSetMap) throws Exception {
		return dao.loadExpiredCarList(entity, certSetMap);
	}
	
	
}
